'''
Created on 23/11/2010

@author: Nami
'''

import pygame
import gameobjects
from gameobjects.vector2 import Vector2
from bk.pygame.ships.game.base.Xml2Obj import *
from bk.pygame.ships.game.EffectLibrary import Effect, EFFECT_TRAIL

#-------------------------------------------------------------------------------

class Trail_Info(object):
    
    order = 0
    image = None
    location = Vector2(0,0)
    
    def __init__(self, order, image, location):
        self.order = order
        self.image = image
        self.location = location

class Effect_Trail(Effect):
    '''
    classdocs
    '''

    trail_images = []
    
    last_trail_order = 0
    
    trail_max = 0
    trail_time_step = 0
    
    current_time_step = 0

    def update(self, elapsed):
        
        Effect.update(self, elapsed)
        
        if self.trail_max <= 0 or self.trail_time_step <= 0:
            return
        
        if not self.parent.scaled_image:
            return
        
        self.current_time_step += elapsed
        if self.current_time_step > self.trail_time_step:
            self.current_time_step -= self.trail_time_step
            self.last_trail_order += 1
            trail_element = Trail_Info(self.last_trail_order,self.parent.scaled_image,Vector2(*self.parent.location))
            self.trail_images.insert(0,trail_element)
            self.trail_images = self.trail_images[:self.trail_max]

    def render(self, surface):
        if len(self.trail_images) == 0:
            return
        
        render_list = list(self.trail_images)
        render_list.reverse()
        trans_step = 185/len(self.trail_images)
        
        current_element = 0
        
        for trail_element in render_list:
            w, h = trail_element.image.get_size()
            #current_element += 1
            #trans_surface = pygame.Surface((w,h))
            #trans_surface.blit(trail_element.image, (0,0))
            #trans_surface.set_alpha(125)
            
            #surface.blit(trans_surface, (trail_element.location.x-w/2, trail_element.location.y-h/2))
            surface.set_alpha(100)
            surface.blit(trail_element.image, (trail_element.location.x-w/2, trail_element.location.y-h/2))
            surface.set_alpha(255)

    def __init__(self,trail_max = 0, trail_time_step = 0, duration = -1):
        '''
        Constructor
        '''
        Effect.__init__(self, EFFECT_TRAIL, duration)
        
        self.trail_max = trail_max
        self.trail_time_step = trail_time_step
        self.duration = duration
        
#-------------------------------------------------------------------------------